<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta name="viewport" content="width=device-width">
  <meta content="" name="Keywords"/>
  <meta content="" name="Description"/>
  <title>荩美国际商城</title>
  <link type="text/css" href="/resources/js/loading/loading.css" rel="stylesheet"/>
  <script type="text/javascript" src="/resources/js/jquery-1.8.1.min.js"></script>
  <script type="text/javascript" src="/resources/js/jquery.bgiframe.js"></script>
  <script type="text/javascript" src="/resources/js/loading/loading.js"></script>

</head>

<body>
<input type="hidden" id="orderId" name="orderId" value="${param.state}"/>
<div id="main"></div>

<script type="text/javascript">
  var myloading = null;
  function jsApiCall(d) {
    //var d = $.parseJSON(data);
    WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            {
              "appId": d.appId,
              "timeStamp": d.timeStamp,
              "nonceStr": d.nonceStr,
              "package": d.package,
              "signType": d.signType,
              "paySign": d.paySign
            },
            function (res) {
              if (res.err_msg == 'get_brand_wcpay_request:ok') {
                alert("支付成功");
              } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                cancelOrder();
                alert("支付取消");
              } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                cancelOrder();
                alert("支付失败");
              } else {
                cancelOrder();
                alert("付款失败，请联系商家！");
              }
             /* var url ="/front/order/payResultBack?orderId=${state}"
                      +"&errorMessage=" + res.err_msg;*/
              window.location.href="/front/user/order/detail?id=${state}";
            }
    );
  }

  function cancelOrder(){
    $.ajax({
      url:"/front/order/cancel?r="+Math.random(),
      data:{
        orderId:$("#orderId").val()
      },
      success:function(res){

      },
      error:function(){}
    })
  }


  function callpay(data) {
    try {
      if (WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', jsApiCall(data), false);
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', jsApiCall(data));
          document.attachEvent('onWeixinJSBridgeReady', jsApiCall(data));
        }
      } else {
        jsApiCall(data);
      }
    } catch (e) {
      alert("请在微信浏览器中打开");
    }
  }

  function loadOpenId(){
    $.ajax({
      url:'/front/order/pay?ran='+Math.random(),
      data:{
        orderId:'${state}',
        code:'${code}'
      },
      dataType:'json',
      type:'post',
      success:function(res){
        myloading.hide();
        if(res.success){
          callpay(res.data);
        }else{
          alert(res.msg);
        }
      }
    });
  }

  function checkWeixinJs(){
    myloading = new ol.loading({
      id : "main",
      msg : "加载支付信息..."
    });
    myloading.show("加载支付信息...");
    try{
      if(WeixinJSBridge){
        loadOpenId();
      }
    }catch(e) {
      alert("您的网速不给力，请返回重新支付");
    }
  }
 document.onreadystatechange = function(){
    if(document.readyState == 'complete'){
      setTimeout('checkWeixinJs()',1500);
    }
  };
</script>

</body>

</html>
